from apps.ext import db
from datetime import datetime

# https://gitee.com/didi123123/flask_47/blob/master/common/models/news.py

# https://www.cnblogs.com/bertwu/p/13434972.html

# 混用sql
# https://cloud.tencent.com/developer/ask/sof/50702

class BaseModel(db.Model):
    __abstract__ = True

    is_del = db.Column(db.Boolean, nullable=True,default=0)
    create_user = db.Column(db.String(50), nullable=True)
    create_time = db.Column(db.DateTime, default=datetime.now, nullable=True)
    update_user = db.Column(db.String(50), nullable=True)
    update_time = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now, nullable=True)

    def toList(self,data):
        print("父类list")
        dataList =[]
        for itm in data:
            dataList.append(itm.to_dict())
        return dataList

    def getListOnce(self,parameters:dict):
        """
        # 多条件查询单个
        :param parameters:
        :return:
        """
        try:
            modelObj = self.__class__.query.filter_by(**parameters).first()

        except Exception as e:
            return None, repr(e)
        if modelObj is None:
            return None,None
        try:
            data = modelObj.to_dict()

        except Exception as e:
            return None, repr(e)

        return data,None

    def getListPageAll(self,parameters:dict,parameters1:dict,page:int=1,per_page:int=50):
        """
        多条件查询列表分页
        :param parameters: 条件字典1
        :param parameters1: 条件字典2
        :param page: 查询页码
        :param per_page: 分页大小
        :return:
        """
        try:
            if parameters=={}:

                modelObj = self.__class__.query.filter_by().paginate(page, per_page, False)
            else:
                modelObj = self.__class__.query.filter_by(parameters).paginate(page, per_page, False)
            result = self.toList(modelObj.items)
        except Exception as e:
            return None, repr(e)
        print(result)
        objData = {}
        objData["total"] = modelObj.total  # 总记录数
        objData["has_next"] = modelObj.has_next  # 是否有下一页
        objData["has_prev"] = modelObj.has_prev  # 是否有上一页
        objData["page"] = modelObj.page  # 当前页
        objData["pages"] = modelObj.pages  # 总页数
        objData["pre_page"] = per_page  # 分页大小
        objData["result"] = result  # 分页大小
        return objData, None

    def getListAll(self,parameters:dict,parameters1:dict,page:int=1,per_page:int=50):
        """
        多条件查询列表
        :param parameters: 条件字典1
        :param parameters1: 条件字典2
        :param page: 查询页码
        :param per_page: 分页大小
        :return:
        """
        try:
            if parameters =={}:
                modelObj = self.__class__.query.all()
            else:
                modelObj = self.__class__.query.filter_by(parameters).all()
            result = self.toList(modelObj)
        except Exception as e:
            return None, repr(e)
        return result, None

    def getPkOnce(self,pk):
        """
        通过id查询单个
        :param pk: id
        :return:
        """
        try:
            modelObj = self.__class__.query.filter_by(id=pk).first()
        except Exception as e:
            return None, repr(e)

        if modelObj is None:
            return None,None
        try:
            data = modelObj.to_dict()

        except Exception as e:
            return None, repr(e)
        return data,None

    def postPkOnce(self,parameters:dict):
        try:
            addData =  self.__class__(**parameters)
            db.session.add(addData)
            db.session.commit()
        except Exception as e:
            return None, repr(e)
        return None,None

    def putPkOnce(self,pk,parameters:dict):
        try:
            person = self.__class__.query.filter_by(id=pk).update(parameters)
            db.session.commit()
        except Exception as e:
            return None, repr(e)
        if person ==0:
            return None,"没有查询到要更新的数据"
        return None,None

    def deletePkOnce(self,pk):
        """
        通过id查询单个
        :param pk: id
        :return:
        """
        try:
            self.__class__.query.filter_by(id=pk).delete()
            db.session.commit()
        except Exception as e:
            return None, repr(e)

        return None,None
